package com.hzp.lolnews.ui.news.view;


import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzp.lolnews.R;
import com.hzp.lolnews.base.BaseFragment;
import com.hzp.lolnews.bean.HomeContent;
import com.hzp.lolnews.bean.HomeGrid;
import com.hzp.lolnews.bean.HomeRow;
import com.hzp.lolnews.configs.Configs;
import com.hzp.lolnews.ui.news.presenter.HomePresenter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends BaseFragment implements IHomeContent{


    private ViewPager mVp;
    private LinearLayout llPoints;
    private TextView tvTitle;
    private HomeHeadAdapter adapter;
    private RecyclerView mRecycler;
    private HomeGridAdapter gridAdapter;
    private XRecyclerView xRecyclerView;
    private HomeAdapter homeAdapter;
    private HomePresenter presenter;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mVp = (ViewPager) view.findViewById(R.id.head_viewpager);
        llPoints = (LinearLayout) view.findViewById(R.id.Lb_ll_points);
        tvTitle = (TextView) view.findViewById(R.id.Lb_tv_title);
        mRecycler = (RecyclerView) view.findViewById(R.id.head_recyclerView);
        xRecyclerView = (XRecyclerView) view.findViewById(R.id.home_recyclerView);
        //导航栏的布局管理器
        GridLayoutManager manager = new GridLayoutManager(getFActivity(), 5);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
            mRecycler.setLayoutManager(manager);
        }
        //内容的recyclerView的布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getFActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
            xRecyclerView.setLayoutManager(linearLayoutManager);
        }
        presenter = new HomePresenter(this);
       //传递数据
        presenter.loadData();
       //设置viewpager，recyclerview的适配器
        adapter = new HomeHeadAdapter();
        gridAdapter = new HomeGridAdapter();
        homeAdapter = new HomeAdapter();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
            mVp.setAdapter(adapter);
        }
        mRecycler.setAdapter(gridAdapter);
        xRecyclerView.setAdapter(homeAdapter);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                presenter.onReFresh();
                xRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                presenter.onLoadMore();
                xRecyclerView.loadMoreComplete();
            }
        });
    }

    //初始化轮播的小圆点
    private void initPoint(final List<HomeRow> list) {
        final int size = list.size();
        for (int i = 0; i < size; i++) {
            View point = new View(getFActivity());
            DisplayMetrics metrics = new DisplayMetrics();
            float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 15, metrics);
            float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 15, metrics);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) width, (int) height);
            params.leftMargin = 5;
            point.setLayoutParams(params);
            point.setBackgroundResource(R.drawable.normal_points);
            assert llPoints != null;
            llPoints.addView(point);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
            llPoints.getChildAt(0).setBackgroundResource(R.drawable.checked_points);
            tvTitle.setText(list.get(0).getTitle());//titles[position % images.size()]
            mVp.setCurrentItem(1000);
            mVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(final int position) {
                    tvTitle.setText(list.get(position % size).getTitle());//titles[position % images.size()]
                    tvTitle.setTextSize(10);
                    tvTitle.setTextColor(Color.WHITE);

                    for (int i = 0; i < llPoints.getChildCount(); i++) {
                        llPoints.getChildAt(i).setBackgroundResource(R.drawable.normal_points);
                        if (i == position % list.size()) {
                            llPoints.getChildAt(position % size).setBackgroundResource(R.drawable.checked_points);
                        }
                    }
                }
            });
        }
    }

    @Override
    public void initView() {

    }

    //关联布局id
    @Override
    public int getLayoutId() {
        return R.layout.fragment_news;
    }




    @Override
    public String[] getPath() {
        return Configs.url;
    }

    @Override
    public String getRefresh() {
        return Configs.urlNews;
    }

    @Override
    public String getLoadMore() {
        return Configs.urlMore;
    }

    @Override
    public void addHomeGrid(List<HomeGrid> list) {
        if (list != null) {
            gridAdapter.setData(list,getFActivity());
        }
    }

    @Override
    public void addHomeRow(List<HomeRow> list) {
        if (list != null) {
            initPoint(list);
            adapter.setData(list);
        }
    }



    @Override
    public void addHomeContent(List<HomeContent> list) {
        if (list != null) {
            homeAdapter.setData(list,getFActivity());
            Log.i("TAG", "addHomeContent:+++++++++++++++ " + list);
        }
    }

    @Override
    public void addRefresh( List<HomeContent> list) {
        Log.i("TAG", "addRefresh:======================>>>> "+ list.size());
        if (list != null){
            homeAdapter.setData(list,getFActivity());
        }
    }

    @Override
    public void addLoadMore( List<HomeContent> list) {
        Log.i("TAG", "addLoadMore:===============================> " + list.size());
           if (list != null){
              // homeAdapter.setData(list,getFActivity());
               homeAdapter.setMoreData(list);
           }
    }

    @Override
    public void showLoadFailMsg(String msg, Throwable e) {

    }


    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }


}
